<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/25
 * Time: 18:31
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\admin\model\QiNiu as Qi;

class Qiniu extends Controller
{
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('qiniu/del')
            ->addTableHeaderAdd('qiniu/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','名称')
            ->addTableColumn('ak','text','七牛ak')
            ->addTableColumn('sk','text','七牛sk')
            ->addTableColumn('bucket','text','七牛空间名称bucket')
            ->addTableColumn('status','switch','状态',['默认','关闭'],'qiniu/change')
            ->addTableRightHeader('操作')
            ->addTableRight('del','qiniu/del','删除')
            ->addTableRight('edit','qiniu/show_edit','编辑')
            ->addTablePage('qiniu/page')
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Qi::where('id','>',0)
            ->order('id desc')
            ->paginate($limit)
            ->toArray();
        return json_page_response($data);
    }
    public function change(Request $request){
        $id = $request->param('id');
        $thisshuju = Qi::where('id',$id)->findOrEmpty()->toArray();
        if($thisshuju['status']==1){
            $thisshuju['status'] = 0;
        }else{
            $thisshuju['status'] = 1;
            $shuju = Qi::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                if($value['status']==1){
                    $value['status'] = 0;
                    Qi::where('id',$value['id'])->update(['status'=>0]);
                }
            }
        }
        if(Qi::where('id',$thisshuju['id'])->update(['status'=>$thisshuju['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }
    /**加载添加
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        return NewBuilder::make('form')
            ->addAction('qiniu/add')
            ->addInput('name','名称')
            ->addInput('ak','七牛云的ak')
            ->addInput('sk','七牛云的sk')
            ->addInput('bucket','七牛云的空间名称')
            ->addInput('url','七牛云域名')
            ->addSwitch('status','是否启用，是否默认')
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
            $shuju = Qi::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                Qi::where('id',$value['id'])->update(['status'=>0]);
            }
        }else{
            $data['status'] = 0;
        }
        if(Qi::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Qi::where('id',$id)->findOrEmpty()->toArray();
        return NewBuilder::make('form')
            ->addAction('qiniu/edit')
            ->addNone('id',$id)
            ->addInput('name','名称',$data['name'])
            ->addInput('ak','七牛云的ak',$data['ak'])
            ->addInput('sk','七牛云的sk',$data['sk'])
            ->addInput('bucket','七牛云的空间名称',$data['bucket'])
            ->addInput('url','七牛云域名',$data['url'])
            ->addSwitch('status','是否启用，是否默认',$data['status'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**修改
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
            $shuju = Qi::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                Qi::where('id',$value['id'])->update(['status'=>0]);
            }
        }else{
            $data['status'] = 0;
        }
        if(Qi::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(Qi::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败',205);
        }
    }
}